#include <iostream>
#include <functional>
#include <time.h>
#include <pthread.h>
#include <vector>
#include <string>
#include <unistd.h>

using fun_t = std::function<void()>;

const int threadnum = 5;

class ThreadData
{
public:
    ThreadData(const std::string &name, const uint64_t &ctime,fun_t f)
    :threadname(name), creattime(ctime), funct(f)
    {}
public:
    std::string threadname;
    uint64_t creattime;
    fun_t funct; 
};

void Print()
{
    std::cout << "我是线程执行的大任务的一部分" << std::endl;
}

void *rountine(void *arg)
{
    ThreadData *td = static_cast<ThreadData *>(arg);
    while(1)
    {
        //int a = 10;
        std::cout << "new thread"
                  <<" thread name:" << td->threadname << "create time:" << td->creattime << std::endl;
        td->funct();
        if(td->threadname == "thread-4")
        {
            std::cout << td->threadname << "触发了异常！！！！" << std::endl;
            //a/=0;

        }  
        sleep(1);
    }
}


int main()
{
    std::vector<pthread_t> pthreads;
    for(size_t i = 0; i <threadnum; i++)
    {
        pthread_t pid;
        char threadName[64];
        snprintf(threadName, sizeof(threadName),"%s-%lu","thread",i);
        ThreadData *Td = new ThreadData(threadName,(uint64_t)time(nullptr),Print);

        pthread_create(&pid, NULL, rountine, Td);

        pthreads.push_back(pid);
        sleep(1);
    }
    std::cout << "thread id:";
    for(const auto &id :pthreads)
    {
        std::cout << id <<",";
    }
    std::cout << std::endl;
    while (true)
    {
        std::cout << "main thread" << std::endl;
        sleep(3);
    }
    return 0;
}